<template>
  <div class="thumbnail_header">
    <el-tabs class="f-render-header" :stretch="true" v-model="activeTab" @tab-click="handleTabClick" v-if="hasForm">
      <el-tab-pane
          v-for="tab of currentPage !== 'payment_page' ? rightTabs : rightTabs2"
          :key="tab.name"
          :label="tab.label"
          :name="tab.name"
      ></el-tab-pane>
<!--      <el-tab-pane-->
<!--          v-else-->
<!--          v-for="(tab, index) of rightTabs2"-->
<!--          :key="index"-->
<!--          :label="tab.label"-->
<!--          :name="tab.name"-->
<!--      ></el-tab-pane>-->
    </el-tabs>
    <div class="wrapper_inner">
      <template v-if="hasForm">
        <el-scrollbar ref="scroll" class="render-scrollarea">
          <div class="thumbnail">
            <div class="thumbnail_wrapper">
              <div class="item" @click="handleSelect(realNav)" :class="{select :  realNav.id === currentId}"
                   v-if="activeTab === 'form-props' && currentPage !== 'payment_page'">
                    <span class="icon-wrapper">
                      <svg-icon icon-class="fixed" class="icon"></svg-icon>
                      <span class="card_name">导航</span>
                    </span>
              </div>
              <div class="page_dragable_wrapper">
                <draggable v-model="realCardList"  animation="500" @end="dragEnd" chosenClass="active"  filter=".forbid"
                           :preventOnFilter="false">
                  <template v-for="(item, index) in realCardList">
                    <div class="page_dragable_item" :key="index" @click="handleSelect(item)"
                         :class="{select : item.id === currentId, forbid: activeTab === 'form-common'}">
                   <span class="icon-wrapper">
                     <svg-icon icon-class="grid"></svg-icon>
                     <span class="card_name">{{ item.cardName }}</span>
                    </span>
                      <i class="el-icon-delete icon" @click.stop="handleDel(item.id)" v-if="activeTab !== 'form-common' && item.typeCode!=='product_detail' && item.typeCode !== 'series_detail' && currentPage !== 'payment_page'"></i>
                    </div>
                  </template>
                </draggable>
              </div>
              <div class="item" @click="handleSelect({typeCode: 'cardList', id: 99998})"
                   :class="{select :  currentId === 99998}" v-if="activeTab === 'form-props' && currentPage !== 'payment_page'">
              <span class="icon-wrapper addCard">
                <i class="el-icon-plus"></i>
                <span class="card_name">添加卡片</span>
              </span>
              </div>
              <div class="item" @click.stop="handleSelect(foot)"
                   :class="{select :  foot.id===currentId }" v-if="activeTab === 'form-props'&& currentPage !== 'payment_page'">
              <span class="icon-wrapper">
                <svg-icon icon-class="fixed" class="icon"></svg-icon>
                <span class="card_name">页尾</span>
              </span>
              </div>
              <div class="item" v-if="footsettings&&footsettings.id&&activeTab === 'form-props'" @click="handleSelect(footsettings)"
                   :class="{select :  footsettings.id===currentId }">
              <span class="icon-wrapper">
                <svg-icon icon-class="fixed" class="icon"></svg-icon>
                <span class="card_name">页面设置</span>
              </span>
              </div>
            </div>
          </div>
        </el-scrollbar>
        <div class="settings" ref="settings">
          <template v-for="(item, index) in rightTabs" class="setting_block">
            <component ref="formCard"  v-on="$listeners" :is="item.activeNav" :key="index"  :value="item.formData" v-if="item.name === activeTab"></component>
          </template>
        </div>
      </template>

      <div class="empty" v-else>
        <img :src="themeEmpty" alt="">
        <h5 class="empty_title">当前页面不支持配置</h5>
        <p class="empty_text">当前页面不支持配置</p>
      </div>
    </div>
  </div>
</template>

<script>
import draggable from 'vuedraggable';
import {deepClone} from '@/utils/utils';
import themeEmpty from '@/assets/images/theme-empty.png';
import navigationForm from '@/views/theme/components/forms/navigationForm';
import slideForm from './forms/slideForm';
import collectionListForm from './forms/collectionListForm';
import collectionSliderForm from '@/views/theme/components/forms/collectionSliderForm';
import featureColumnsForm from '@/views/theme/components/forms/featureColumnsForm';
import featureProductForm from '@/views/theme/components/forms/featureProductForm';
import fourImages from '@/views/theme/components/forms/fourImages';
import galleryForm from '@/views/theme/components/forms/galleryForm';
import imageText from '@/views/theme/components/forms/imageText';
import overlayImage from '@/views/theme/components/forms/overlayImage';
import productsForm from '@/views/theme/components/forms/productsForm';
import richTextForm from '@/views/theme/components/forms/richTextForm';
import threeImagesForm from '@/views/theme/components/forms/threeImagesForm';
import twoImagesForm from '@/views/theme/components/forms/twoImagesForm';
import footerForm from '@/views/theme/components/forms/footerForm';
import commonForm from '@/views/theme/components/forms/globalCommonForm';
import globalColorForm from '@/views/theme/components/forms/globalColorForm';
import globalFontForm from '@/views/theme/components/forms/globalFontForm';
import globalProductForm from '@/views/theme/components/forms/globalProductForm';
import globalSocialForm from '@/views/theme/components/forms/globalSocialForm';
import cardListForm from '@/views/theme/components/forms/cardListForm';
import seriesDetailForm from '@/views/theme/components/forms/seriesDetailForm';
import productDetailForm from '@/views/theme/components/forms/productDetailForm';
import paymentForm from '@/views/theme/components/forms/paymentForm';
import paymentFooterForm from '@/views/theme/components/forms/paymentFooterForm';
import footerSettingsForm from '@/views/theme/components/forms/footerSettingsForm';
import brandListForm from '@/views/theme/components/forms/brandListForm';
import htmlForm from '@/views/theme/components/forms/htmlForm';
import InsCollectionForm from '@/views/theme/components/forms/InsCollectionForm';
import textVideoForm from '@/views/theme/components/forms/textVideoForm';
import videoForm from '@/views/theme/components/forms/videoForm';
import featuredCollectionFourForm from '@/views/theme/components/forms/featuredCollectionFourForm';
import productSlideForm from '@/views/theme/components/forms/productSlideForm.vue';
import InsSimpleCollectionForm from '@/views/theme/components/forms/InsSimpleCollectionForm.vue';
import commentForm from '@/views/theme/components/forms/commentForm.vue';

export default {
    components: {
        draggable,
        'fix_header': navigationForm,
        'fix_footer': footerForm,
        'common': commonForm,
        'color': globalColorForm,
        'font': globalFontForm,
        'product': globalProductForm,
        'social': globalSocialForm,
        'slide_show': slideForm, // 轮播图
        'series_list': collectionListForm, // 专辑列表
        'product_switch': collectionSliderForm, // 商品切换
        'image_text_list': featureColumnsForm, // 图文列表
        'single_product': featureProductForm, // 单个商品
        'image_four': fourImages, // 多图-4
        'single_image': galleryForm, // 单排图片
        'image_text': imageText, // 图文
        'image_one': overlayImage, // 单图片
        'series_product': productsForm, // 专辑商品
        'rich_text': richTextForm, // 富文本
        'image_three': threeImagesForm, // 多图-3
        'image_two': twoImagesForm, // 多图-2
        'cardList': cardListForm, // 添加卡片
        'series_detail': seriesDetailForm, // 系列详情
        'product_detail': productDetailForm, // 商品详情
        'payment_detail': paymentForm,
        'payment_footer': paymentFooterForm,
        'brand_list': brandListForm,
        'html_card': htmlForm,
        'ins_card': InsCollectionForm,
        'ins_simple_card': InsSimpleCollectionForm,
        'text_video': textVideoForm,
        'video_card': videoForm,
        'seo': footerSettingsForm,
        'featured_collection_four': featuredCollectionFourForm,
        'product_slide': productSlideForm,
        'comment': commentForm

    },
    props: {
        nav: {
            type: Object,
            default: () => {}
        },
        foot: {
            type: Object,
            default: () => {}
        },
        footsettings: {
            type: Object,
            default: () => {}
        },
        cardList: {
            type: Array,
            default: () => []
        },
        available: {
            type: Array,
            default: () => []
        },
        hasForm: {
            type: Boolean,
            default: true
        },
        currentPage: {
            type: String,
            default: ''
        }
    },
    computed: {
        realNav () {
            return this.nav;
        }
    },
    watch: {
        cardList: {
            handler (value) {
                this.pageCount++;
                if (this.pageCount === 2) {
                    this.handleSelect(this.realNav);
                    this.realCardList = deepClone(value);
                }
                if (this.pageCount > 2) {
                    this.realCardList = deepClone(value);
                }
            },
            immediate: false,
            deep: true
        }
    },
    data () {
        return {
            pageCount: 0,
            themeEmpty,
            currentId: 9999,
            activeNav: 'fix_header',
            activeTab: 'form-props',
            rightTabs: [
                {
                    name: 'form-props',
                    label: '卡片装修',
                    activeNav: 'fix_header',
                    formData: null
                },
                {
                    name: 'form-common',
                    label: '全局设置',
                    activeNav: 'common',
                    formData: null
                }
            ],
            rightTabs2: [{
                name: 'form-props',
                label: '卡片装修',
                activeNav: 'fix_header',
                formData: null
            }],
            contentId: [],
            realCardList: this.cardList
        };
    },
    destroyed () {
        this.pageCount = 0;
    },
    methods: {
        handleSelect (item, flag = true) {
            const {typeCode, id} = item;
            this.currentId = id;
            if (this.activeTab === 'form-props') {
                this.rightTabs[0].activeNav = typeCode;
                if (typeCode === 'cardList') {
                    this.rightTabs[0].formData = this.available;
                } else {
                    this.rightTabs[0].formData = item;
                }
            } else {
                this.rightTabs[1].activeNav = typeCode;
                const commonFormData = this.cardList.filter(function (item) { return item.id === id; });
                this.rightTabs[1].formData = commonFormData[0];
            }
            if (flag) {
                this.$nextTick(() => {
                    if (this.$refs.settings) {
                        this.$refs.settings.scrollTop = 0;
                    }
                });
            }
            this.$emit('changeCart', item);
        },
        handleDel (id) {
            this.$emit('deleteCard', id);
            // this.$store.commit('DEL_ADD_CARD_ID', id);
        },
        handleTabClick (val) {
            this.$emit('tabClick', val.name);
        },
        dragEnd () {
            const data = this.realCardList.map(res => res.id);
            this.$emit('sortList', {cardDTOList: data});
        },
        resetCard (id) {
            setTimeout(() => {
                const data = [...this.realCardList, this.realNav, this.foot, this.footsettings];
                const row = data.find(res => res.id === id);
                this.handleSelect(row, false);
            }, 400);
        },
        setDefault () {
            setTimeout(() => {
                if (this.activeTab === 'form-common') {
                    this.handleSelect(this.cardList[0]);
                } else {
                    if (this.currentPage !== 'payment_page') {
                        this.handleSelect(this.realNav);
                        if (this.$refs.formCard.length) {
                            this.$refs.formCard[0].pageCount = 0;
                        }
                    } else {
                        this.handleSelect(this.cardList[0]);
                    }
                }
            }, 800);
        }
    }
};
</script>

<style scoped lang="scss">
.wrapper_inner {
  width: 440px;
  position: absolute;
  top: 56px;
  bottom: 0;
  right: 0;

  .render-scrollarea {
    height: 100%;
  }

  .settings {
    width: 300px;
    height: 100%;
    position: absolute;
    top: 0;
    right: 0;
    overflow: auto;
    z-index: 2;
  }
}

.thumbnail {
  height: 100%;
  width: 140px;
  position: relative;
  background: #eef0f2;
  border-right: 1px solid #c5cddb;
  border-left: 1px solid #c5cddb;

  .thumbnail_wrapper {
    position: absolute;
    width: calc(100% + 1px);
    top: 0;
    bottom: 0;
    overflow-x: hidden;
    overflow-y: auto;

    .item {
      display: flex;
      justify-content: space-between;
      padding: 0 10px;
      box-sizing: border-box;
      color: #383838;
      font-weight: 400;
      height: 56px;
      cursor: pointer;
      -webkit-box-align: center;
      -webkit-align-items: center;
      -ms-flex-align: center;
      align-items: center;
      user-select: none;
      border-top: 1px solid #c5cddb;
      font-size: 14px;

      .icon {
        color: #abb6c9;
      }

      &.select {
        background: #fff;
        color: #212121 !important;
        font-weight: 500 !important;
        position: relative;
        width: calc(100% + 1px);

        &:after {
          position: absolute;
          content: '';
          display: block;
          height: 100%;
          width: 4px;
          background: #5b99ea;
          top: 0;
          left: -1px;
        }
      }

      .icon-wrapper {
        display: flex;
        align-items: center;
        font-size: 14px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        &.addCard{
          color: #2e60f8;
        }
      }
    }

    .page_dragable_item {
      display: flex;
      justify-content: space-between;
      padding: 0 10px;
      box-sizing: border-box;
      color: #383838;
      font-weight: 400;
      height: 56px;
      cursor: pointer;
      -webkit-box-align: center;
      -webkit-align-items: center;
      -ms-flex-align: center;
      align-items: center;
      user-select: none;
      border-top: 1px solid #c5cddb;
      font-size: 14px;

      &.select {
        background: #fff;
        color: #212121 !important;
        font-weight: 500 !important;
        position: relative;
        width: calc(100% + 1px);

        &:after {
          position: absolute;
          content: '';
          display: block;
          height: 100%;
          width: 4px;
          background: #5b99ea;
          top: 0;
          left: -1px;
        }
      }

      .icon {
        display: none;
      }

      &:hover {
        .icon {
          display: block;
        }
      }
    }
  }
}

.empty {
  position: absolute;
  top: 0;
  right: 0;
  width: 440px;
  bottom: 0;
  background: #eef0f2;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  flex-direction: column;

  .empty_title {
    padding: 0;
    margin: 0;
    font-size: 16px;
  }

  .empty_text {
    font-size: 14px;
    color: #999;
  }
}
</style>

<style lang="scss">
.thumbnail_header{
  .el-tabs__item {
    height: 56px;
    line-height: 56px;
    color: #212212;
    background-color: #eef0f2;
    font-size: 16px;
    font-weight: 500;
    text-align: center;
    cursor: pointer;
  }
  .el-tabs__active-bar {
    height: 3px;
  }
  .render-scrollarea {
    .el-scrollbar__view {
      height: 100%;
    }
  }
  .ele-form-section-content {
    margin: 0 10px;
    .ele-form-section-header {
      background: #f6f7f9;
      margin: 0 -20px 15px;
      padding: 19px 20px;
    }
  }
  .ele-form-section .el-form {
    border: none;
  }
  .widget_settings_pro .el-slider {
    width: 222px;
    margin: 0 9px;
  }
  .widget_settings_pro .el-form-item .el-form-item__content {
    .el-input {
      width: 100%;
      margin-top: 5px;
    }
  }
}

</style>
